package cs236369.webserver;

import java.util.HashMap;

public class SessionMap {
	private HashMap<String, Session> sessionMap;
	int counter = 0;
	
	SessionMap() {
		sessionMap = new HashMap<String, Session>();
	}
	
	synchronized String addSession(Session session) {
		counter++;;
		String key = String.valueOf(counter);
		sessionMap.put(key, session);
		return key;
	}
	
	synchronized Session getSession(String key) {
		return sessionMap.get(key);
	}
	
	public static String getSessionKey(String cookieContent) {
		String[] lines = cookieContent.split(";");
		for (String line:lines) {
			if (line.indexOf("=") == -1) {
				continue;
			}
			String[] parts = line.split("=");
			if (parts[0].trim().equals("sessionkey")) {
				return parts[1].trim();
			}
		}
		return null;
	}
}
